package server.thread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

/**
 * Realiza o parse da requisição recebida
 *
 * @author Thiago Galbiatti Vespa - <a
 * href="mailto:thiago@thiagovespa.com.br">thiago@thiagovespa.com.br</a>
 * @version 0.2
 */
public class Request {

    private String method;
    private String uri;
    private String protocol;
    private String resto;
    private String host;
    private String referer;
    private String contentType;
    private String boundary;
    private int contentLength;
    private boolean d;
    private String data;

    public Request() {}

    /**
     * Realiza o parse
     *
     * @param input
     * @return
     * @throws IOException
     */
    public void parse(String input) throws IOException {
        BufferedReader br = new BufferedReader(new StringReader(input));
        String line = null;
        int lineNumber = 0;
        
        String host = "Host:";
        String referer = "Referer:";
        String contentType = "Content-Type:";
        String contentLength = "Content-Length:";
        
        data = "";
        while ((line = br.readLine()) != null) {
            //System.out.println(lineNumber + " " + line);
            if (lineNumber == 0) {
                String[] values = line.split(" ");
                if (values.length == 3) {
                    this.method = values[0];
                    this.uri = values[1];
                    this.protocol = values[2];
                }// TODO Tratar erro
            } else {
                resto = resto + line + "; ";
                String[] values = line.split(" ");
                if (values[0].equalsIgnoreCase(host)) {
                    this.host = values[1];
                }
                if (method.equalsIgnoreCase("POST")) {
                    if (values[0].equalsIgnoreCase(referer)) {
                        this.referer = values[1];
                    }
                    if (values[0].equalsIgnoreCase(contentType)) {
                        this.contentType = values[1];
                        if (values[1].indexOf("multipart/form-data") != -1) {
                            boundary = values[2];
                        }
                    } else {
                        if (values[0].equalsIgnoreCase(contentLength)) {
                            this.contentLength = Integer.parseInt(values[1]);
                            d = true;
                        } else {
                            if (d) {
                                data = data + line;
                            }
                        }
                    }
                }
            }
            if (d) {
                String temp = "";
                for (int i = 0; i < data.length(); i++) {
                    if (data.charAt(i) == '+') {
                        temp = temp + " ";
                    } else {
                        temp = temp + data.charAt(i);
                    }
                }
                data = temp;
            }
            lineNumber++;
        }
    }

    public String toString() {
        return "metodo: " + method + "\n"
                + "url: " + uri + "\n"
                + "protocol: " + protocol + "\n"
                + "host: " + host + "\n" +
                ((method.equalsIgnoreCase("POST")) ? 
                ("referer: " + referer + "\n"
                + "cntent-type: " + contentType + " " + boundary + "\n"
                + "cotent-length: " + contentLength + "\n"
                + "data: " + data + "\n") : "");
    }
    
    /**
     * Recupera o método
     *
     * @return metodo TODO: Converter para Enum
     */
    public String getMethod() {
        return method;
    }

    /**
     * Recupera a URI
     *
     * @return uri
     */
    public String getUri() {
        return uri;
    }

    /**
     * Recupera o protocolo
     *
     * @return TODO: Fazer validação
     */
    public String getProtocol() {
        return protocol;
    }

    public String getResto() {
        return resto;
    }

    public int getContentLength() {
        return contentLength;
    }

    public String getContentType() {
        return contentType;
    }

    public String getBoundary() {
        return boundary;
    }

    public String getData() {
        return data;
    }

    public String getHost() {
        return host;
    }

    public String getReferer() {
        return referer;
    }
    
    
}
